﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace DAO
{
    public class Phi_DAO
    {
        private static Phi_DAO m_instance = null;

        public static Phi_DAO Instance
        {
            get
            {
                if (m_instance == null)
                    m_instance = new Phi_DAO();
                return m_instance;
            }
        }

        /// <summary>
        ///   lay thong tin cac khoan phi
        /// </summary>
        /// 
        public DataTable LayThongTin()
        {
            DataTable result = new DataTable();
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAYTOANBOPHI", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(result);
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        ///   lay toan bo ma phi
        /// </summary>
        /// 

        public DataTable LayToanBoMaPhi()
        {
            DataTable result = new DataTable();
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAYTOANBOMAPHI", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                SqlDataAdapter dt = new SqlDataAdapter(cmd);
                dt.Fill(result);
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

        /// <summary>
        ///   Lay Thong Tin phi bang id
        /// </summary>
        /// 

        public DataTable LayThongTinPhi(string _id)
        {
            DataTable result = new DataTable();
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("LAYPHIBYID", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@maphi", SqlDbType.NVarChar, 10);
                cmd.Parameters["@maphi"].Value = _id;

                SqlDataAdapter dt = new SqlDataAdapter(cmd);
                dt.Fill(result);
                return result;
            }
            catch (System.Exception ex)
            {
                return null;
            }
        }

       /// <summary>
       ///   Them Khoan Phi Moi
       /// </summary>
       /// 

        public bool ThemPhi(string _name)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("ADD_PHI_MOI", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@tenphi", SqlDbType.NVarChar, 100);
                cmd.Parameters["@tenphi"].Value = _name;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        ///   Sua Khoan Phi
        /// </summary>
        /// 

        public bool SuaPhi(string _id, string _name)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("UPDATE_PHI", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@maphi", SqlDbType.NVarChar, 10);
                cmd.Parameters["@maphi"].Value = _id;

                cmd.Parameters.Add("@tenphi", SqlDbType.NVarChar, 100);
                cmd.Parameters["@tenphi"].Value = _name;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        ///   xoa Khoan Phi
        /// </summary>
        /// 
        public bool XoaPhi(string _id)
        {
            try
            {
                SqlConnection cnn = SQLConnection.Instance.SqlConnection;
                SqlCommand cmd = new SqlCommand("REMOVE_PHI", cnn);
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@maphi", SqlDbType.NVarChar, 100);
                cmd.Parameters["@maphi"].Value = _id;

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }

    }
}
